package com.itboye.zhujia.login;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.network.HttpRequest;
import com.itboye.zhujia.util.T;
import com.itboye.zhujia.volley.CodeErrorException;
import com.itboye.zhujia.volley.MyJsonRequest;
import com.itboye.zhujia.volley.XErrorListener;
import com.itboye.zhujia.volley.XRequestListener;
/**
 * 注册第一步（填写手机号）
 *
 */
public class RegisterActivityOne extends BaseActivity implements OnClickListener{
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	EditText resster_one_tel;//手机号
	TextView register_one_ok;//下一步
	
	private String  phone;
	
	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_register_one);
		
		add_shap_title_tv.setText("个人房东实名认证");
		phone=resster_one_tel.getText().toString();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;
		case R.id.register_one_ok://下一步
			phone=resster_one_tel.getText().toString();
			if(TextUtils.isEmpty(phone)){
				T.showShort(RegisterActivityOne.this, "手机号码不能为空");
				return;
			}
			if(!isMobileNO(phone)){
				T.showShort(RegisterActivityOne.this, "请输入正确的电话号码！");
				return;
			}
			getyzm();
			
			break;
			
		default:
			break;
		}
		
	}
	
	/**
	 * 获取验证码
	 */
	private void getyzm() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", resster_one_tel.getText().toString())
				.param("code_type", "1")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(RegisterActivityOne.this, response);
						Intent intent=new Intent(RegisterActivityOne.this,RegisterActivityTwo.class);
						intent.putExtra("phone", phone);
						startActivity(intent);
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(RegisterActivityOne.this, msg);
						} else {
							T.showLong(RegisterActivityOne.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	/** 判断是否为手机号 */
	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
}
